package com.study.controller;

import com.study.domain.UserManager;
import com.study.service.UserManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;


@Controller
@RequestMapping("/userManager")
public class UserManagerController {

    @Autowired
    private UserManagerService userManagerService;

    @RequestMapping("/Home")
    public ModelAndView userManagerLogin(HttpServletRequest request, String username, String password){
        ModelAndView mav=new ModelAndView();
        UserManager result = userManagerService.selectUserManager(username,password);
        if(result!=null){
            request.getSession().setAttribute("UserManager",result);
            System.out.println(result);
            mav.setViewName("home");
            return mav;
        }
        //说明查询结果为空，查无此人，重新登录，并给予提示
            mav.addObject("login_field","用户名或密码错误");
            mav.setViewName("loginError");
            return mav;
    }

    @ResponseBody
    @RequestMapping("/changePwd.do")
    public Integer changePassword(HttpServletRequest request){
        UserManager mag = (UserManager)request.getSession().getAttribute("UserManager");
        System.out.println(mag);
        String pwd=request.getParameter("password");
        System.out.println(pwd);
        mag.setPassword(pwd);
        System.out.println(mag);
        int i = userManagerService.modifyPassword(mag);
        if(i==1){
            return 1;
        }
      return 0;

    }
    @ResponseBody
    @RequestMapping("/userLogout.do")
    public Integer UserLogout(HttpServletRequest request){
            //打印当前用户信息,前提是要有前端传来的参数
            /*String str=request.getParameter("nowUser");
            System.out.println(str);*/
            request.getSession().removeAttribute("UserManager");
        return 1;
    }
}
